package ee.ttu.rakarh.model.data.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.User ;
import ee.ttu.rakarh.util.HibernateUtil;

public class UserAuthDAOImpl implements UserAuthDAO{
	private static final Logger LOG = Logger.getLogger(UserAuthDAOImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public User getUser(String username, String passw) {
		List<User> users = new ArrayList<User>();
		User user = null;
		Session session = null;
		try {
			LOG.info("login username=" + username + " password=" + passw);
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			Map<String, String> logPas = new HashMap<String, String>();
			logPas.put("username", username);
			logPas.put("passw", passw);
			
			Criteria crit = session.createCriteria(User.class);
			users = crit.add(Restrictions.allEq(logPas)).list();
			
			if(users.size() > 0){
				LOG.info("nawel uzerov");
				user = users.get(0);
			}else{
				LOG.info("pi4al' =(");
			}

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return user;
	}
}